/*
 * Copyright (C) 2014 Tolstykh Vladislav
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package lib.sort;

import java.util.Comparator;
import lib.scripture.Chapter;
import lib.scripture.Title;
import lib.scripture.Verse;

/**
 * Сортировка тримультимэп.
 * @author Толстых Владислав
 */
public class Sortable implements Comparator<Object>{
    private final int OBJ_ID;
    
    /**
     * Сортируем результаты поиска.
     * @param objId порядковый номер сортируемого объекта (определён в Sort)
     */
    public Sortable(int objId) {
        this.OBJ_ID=objId;
    }

    @Override
    public int compare(Object obj1, Object obj2) {
        
        switch(OBJ_ID){
            case Sort.TITLE:
                return byDefault(((Title)obj1).id(),((Title)obj2).id());
            case Sort.CHAPTER:
                return byDefault(((Chapter)obj1).id(),((Chapter)obj2).id());                
            case Sort.VERSE:
                return byDefault(((Verse)obj1).id(),((Verse)obj2).id());
            case Sort.AS_IS:
                return 1;
        }
        return 0;
    }
    
    /**
     * Сортировка по умолчанию.
     * @param a
     * @param b
     * @return возрастающий список
     */
    private int byDefault(int a, int b){
        if(a > b){
            return 1;
        }else if(a < b){
            return -1;
        }else{
            return 0;
        }
    }
    
}
